/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package exercicioarquivo;

import java.awt.image.BufferedImage;
import java.awt.image.Raster;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class ExercicioArquivo {

    /*   public static void main(String[] args) {
    criptografa();
    }*/

    public static void criaPastas(String pastas) {
        new File(pastas).mkdirs();
    }

    public static void criptografa() {
        File arqFoto = new File("C:/furb_temp/Desert.jpg");
        System.out.println("Data do arquivo: " + arqFoto.lastModified());
        System.out.println("Tamanho do arquivo: " + arqFoto.length());

        try {
            FileInputStream fis = new FileInputStream(arqFoto);

            byte[] dados = new byte[1024];

            File arqCripto = new File("C:/furb_temp/cript.cpt");
            FileOutputStream fos = new FileOutputStream(arqCripto);
            String head = "CPT";

            fos.write(head.getBytes());//cpt
            fos.write(arqFoto.getName().length());//tamanho do nome
            fos.write(arqFoto.getName().getBytes());//bytes para cada char

            while (fis.read(dados) > 0) {
                for (int i = 0; i < dados.length; i++) {
                    dados[i] = new Integer(~dados[i]).byteValue();
                }
                fos.write(dados);
            }
            fis.close();
            fos.close();

        } catch (FileNotFoundException ex) {
            Logger.getLogger(ExercicioArquivo.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ExercicioArquivo.class.getName()).log(Level.SEVERE, null, ex);
        }

        criaPastas("C:/pastateste/poo2");
        File arqTeste = new File("C:/pastateste/teste/doc.txt");

        arqTeste.getParentFile().mkdirs();

        try {
            PrintWriter pw = new PrintWriter(arqTeste);
            pw.write("Hello!");
            pw.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ExercicioArquivo.class.getName()).log(Level.SEVERE, null, ex);
        }

        
        
    }
    public static void Imagem(String[] args) {
                
        try {
            File arqfoto = new File("C:/furb_temp/Desert.jpg");
            BufferedImage origem = ImageIO.read(arqfoto);
            BufferedImage destino = new BufferedImage(origem.getWidth(), origem.getHeight(),0);
            
            Raster raster = origem.getData();
            
            
            for (int i = 0; i < raster.getHeight(); i++) {
                for (int j = 0; j < raster.getWidth(); j++) {
                    
                    int [] pixel = new int[3];
                    
                    raster.getPixel(j, i, pixel);
                    int media  = (pixel[0] + pixel[1]+pixel[2])/3;
                    pixel[0] =pixel[1]=pixel[2] = media;
                    destino.getRaster().setPixel(j, i, pixel);
                                        
                }
                
            }
            ImageIO.write(destino, "jpg", arqfoto);
            
            
        } catch (IOException ex) {
            Logger.getLogger(ExercicioArquivo.class.getName()).log(Level.SEVERE, null, ex);
        }
        
                
    }

}
